
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include "lv_draw.h"
#include "font.h"
#include "touch.h"

// extern volatile int run_chinese;

#define CALENDAR_WIDTH 800
#define CALENDAR_HEIGHT 480
void draw_cendar();
// 日历全局变量
volatile int calendar_running = 0;
volatile int current_year = 2025;
volatile int current_month = 7; // 1-12

extern volatile int run_chinese;

volatile int exit_calendar = 0;

// 触摸处理线程函数
void *calendar_touch_thread(void *arg)
{
    int touch_fd = open("/dev/input/event0", O_RDONLY);
    if (touch_fd == -1)
    {
        perror("open touch device failed");
        return NULL;
    }

    struct input_event event;
    int x = 0, y = 0;

    while (calendar_running)
    {
        if (read(touch_fd, &event, sizeof(event)) != sizeof(event))
        {
            perror("read touch event failed");
            continue;
        }

        if (event.type == EV_ABS)
        {
            if (event.code == ABS_X)
            {
                x = event.value * 800 / 1024;
            }
            else if (event.code == ABS_Y)
            {
                y = event.value * 480 / 600;
            }
        }

        if (event.type == EV_KEY && event.code == BTN_TOUCH)
        {
            if (event.value == 1)
            { // 按下
                touch_x = x;
                touch_y = y;
            }
            else if (event.value == 0)
            { // 释放
                if (touch_x != -1 && touch_y != -1)
                {
                    touch_release = 1;
                }
            }
        }

        usleep(10000); // 10ms延迟
    }

    close(touch_fd);
    return NULL;
}

// 判断某年某月第一天是星期几（0=周日,1=周一...6=周六）
int get_first_weekday(int year, int month)
{
    struct tm timeinfo = {0};
    timeinfo.tm_year = year - 1900;
    timeinfo.tm_mon = month - 1;
    timeinfo.tm_mday = 1;
    mktime(&timeinfo);
    return timeinfo.tm_wday;
}

// 获取某月的天数
int get_days_in_month(int year, int month)
{
    if (month == 2)
    {
        return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
    }
    const int days[] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    return days[month];
}

// 绘制日历界面
void draw_calendar()
{
    lv_fill_clean_bmp(0, 0, 800, 480, RGB(255, 255, 255)); // 白色背景
    lv_clear_fill(RGB(240, 240, 245));                // 浅灰色背景

    // 顶部标题
    char title[50];
    sprintf(title, "%d年%d月日历", current_year, current_month);
    lv_draw_string_bmp(title, 300, 20, RGB(30, 30, 120), 36);

    // 绘制星期标题
    const char *weekdays[] = {"日", "一", "二", "三", "四", "五", "六"};
    for (int i = 0; i < 7; i++)
    {
        int x = 100 + i * 90;
        lv_draw_string_bmp(weekdays[i], x + 35, 70,
                        (i == 0 || i == 6) ? RGB(200, 0, 0) : RGB(0, 0, 0), 28);
    }

    // 获取第一天是星期几
    int first_weekday = get_first_weekday(current_year, current_month);
    int days_in_month = get_days_in_month(current_year, current_month);

    // 绘制日期格子
    int day = 1;
    for (int row = 0; row < 5; row++)
    {
        for (int col = 0; col < 7; col++)
        {
            int x = 100 + col * 90;
            int y = 110 + row * 60; // 原来60

            // 绘制日期格子背景
            unsigned int bg_color = RGB(255, 255, 255);
            if (col == 0 || col == 6)
            { // 周末
                bg_color = RGB(240, 245, 255);
            }
            lv_fill_clean_bmp(x, y, 80, 50, bg_color);

            // 绘制日期
            if ((row == 0 && col < first_weekday) || day > days_in_month)
            {
                continue;
            }

            char day_str[3];
            sprintf(day_str, "%d", day);
            // 居中处理修正：根据字符数计算偏移量
            int text_width = (strlen(day_str) == 1) ? 16 : 32;
            lv_draw_string_bmp(day_str, x + (80 - text_width) / 2, y + 10,
                            (col == 0 || col == 6) ? RGB(200, 0, 0) : RGB(0, 0, 0), 28);

            day++;
        }
    }
    // 绘制退出按钮 (右下角)
    lv_fill_clean_bmp(650, 400, 120, 60, RGB(220, 80, 80));
    lv_draw_string_bmp("退出", 680, 415, RGB(255, 0, 0), 30);

    // 绘制月份切换按钮（位置保持不变）
    lv_fill_clean_bmp(200, 400, 120, 60, RGB(100, 150, 220));
    lv_draw_string_bmp("上个月", 210, 415, RGB(255, 0, 0), 30);

    lv_fill_clean_bmp(450, 400, 120, 60, RGB(100, 150, 220));
    lv_draw_string_bmp("下个月", 460, 415, RGB(255, 0, 0), 30);
}

// 主日历函数
void show_calendar()
{
    pthread_t touch_tid;
    calendar_running = 1;

    // 创建触摸线程
    if (pthread_create(&touch_tid, NULL, calendar_touch_thread, NULL) != 0)
    {
        perror("创建日历触摸线程失败");
        return;
    }
    // lv_fill_clean_bmp(0, 0, 800, 480, RGB(255, 255, 255));
    // 初始绘制日历
    draw_calendar();

    while (calendar_running && !exit_calendar)
    {
        if (touch_release)
        {
            touch_release = 0;

            // 检查点击位置
            if (touch_x >= 650 && touch_x <= 770 && touch_y >= 400 && touch_y <= 460)
            {
                // 退出按钮
                exit_calendar = 1;
            }
            else if (touch_x >= 200 && touch_x <= 320 && touch_y >= 400 && touch_y <= 460)
            {
                // 上个月
                current_month--;
                if (current_month < 1)
                {
                    current_month = 12;
                    current_year--;
                }
                draw_calendar();
            }
            else if (touch_x >= 450 && touch_x <= 570 && touch_y >= 400 && touch_y <= 460)
            {
                // 下个月
                current_month++;
                if (current_month > 12)
                {
                    current_month = 1;
                    current_year++;
                }
                draw_calendar();
            }
        }

        usleep(10000); // 10ms延迟
    }

    // 清理资源
    calendar_running = 0;
    exit_calendar = 0;
    pthread_join(touch_tid, NULL);
}



